package com.axend.aerosense.dev.adapter;

import android.content.res.Resources;
import android.view.View;

import com.axend.aerosense.common.bean.BindRoomBean;
import com.blankj.utilcode.util.LogUtils;
import com.axend.aerosense.common.adapter.BaseBindingAdapter;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.databinding.DevLayoutRoomItemBinding;

import java.util.List;

/**
 * @author ChenHongWei
 * @date :2021/1/12 10:36
 */

public class DevRoomListAdapter extends BaseBindingAdapter<BindRoomBean, DevLayoutRoomItemBinding> {

    private OnItemClickListener onItemClickListener;
    private int current = 0;

    public DevRoomListAdapter(List<BindRoomBean> items) {
        super(items);
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.dev_layout_room_item;
    }

    @Override
    protected void onBindItem(DevLayoutRoomItemBinding binding, BindRoomBean item, int position) {
        Resources resources = binding.devRoomName.getContext().getResources();
        binding.devRoomName.setText(item.getRoomName());
        binding.devDeviceCount.setText(String.format(resources.getString(R.string.dev_one_device), "" + item.getNum()));
        if (current == position){
            binding.devRoomName.setTextColor(resources.getColor(R.color.common_item_selector, null));
            binding.devCheckbox.setVisibility(View.VISIBLE);
        }else {
            binding.devCheckbox.setVisibility(View.GONE);
            binding.devRoomName.setTextColor(resources.getColor(R.color.base_text_color_black, null));
        }
        binding.devItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null){
                    onItemClickListener.onClick(item, position);
                }
                current = position;
                notifyDataSetChanged();
                //upDateList(items);
            }
        });
    }

    @Override
    public void upDateList(List<BindRoomBean> dateItems) {
        super.upDateList(dateItems);
    }

    public BindRoomBean getCustomBindRoomBean(){
        if (null != items && items.size() > 0){
            return items.get(current);
        }
        return null;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClick(BindRoomBean item, int position);
    }
}
